<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="报障单号" prop="reportId">
        <el-input
          v-model="queryParams.reportId"
          placeholder="请输入报障单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="报障单状态" prop="reportStatus">
        <el-select v-model="queryParams.reportStatus" placeholder="请选择报障单状态" clearable size="small" multiple
                   collapse-tags>
          <el-option
            v-for="dict in dict.type.report_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="reportTitle">
        <el-input
          v-model="queryParams.reportTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:ReportOrder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ReportOrderList">
      <el-table-column label="报障单号" align="center" prop="reportId" width="200px"/>
      <el-table-column label="故障地点" align="center" prop="reportLocation"/>
      <el-table-column label="标题" align="center" prop="reportTitle"/>
      <el-table-column label="故障大类" align="center" prop="reportTypeBigName"/>
      <el-table-column label="故障小类" align="center" prop="reportTypeSmallName"/>
      <el-table-column label="故障描述" align="center" prop="reportDesc"/>
      <el-table-column label="报障单状态" align="center" prop="reportStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_status" :value="scope.row.reportStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="reportUserName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="dealOrder(scope.row.id)"
            v-if="scope.row.reportStatus == 'DEALING' || scope.row.reportStatus == 'BACKING' "
          >处理
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="dealTransfer(scope.row.id)"
            v-if="scope.row.reportStatus == 'DEALING' || scope.row.reportStatus == 'BACKING' "
          >转移
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="详情" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="历史记录" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="handleHistory(scope.row)"
          >历史
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <report-time-line :historyDrawerOpen.sync="historyDrawerOpen" :time-line-item-list="timeLineItemList"/>
    <el-dialog :visible.sync="reportOrderDetailOpen" width="800px">
      <el-descriptions class="margin-top" title="报障单详情" :column="3" size="mini" border>
        <el-descriptions-item label="报障人">
          {{ reportOrderDetail.reportUserName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障地点">
          {{ reportOrderDetail.reportLocation }}
        </el-descriptions-item>
        <el-descriptions-item>
        </el-descriptions-item>
        <el-descriptions-item label="手机号">
          {{ reportOrderDetail.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          {{ reportOrderDetail.email }}
        </el-descriptions-item>
        <el-descriptions-item label="qq号">
          {{ reportOrderDetail.qq }}
        </el-descriptions-item>
        <el-descriptions-item label="故障标题">
          {{ reportOrderDetail.reportTitle }}
        </el-descriptions-item>
        <el-descriptions-item label="故障大类">
          {{ reportOrderDetail.reportTypeBigName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障小类">
          {{ reportOrderDetail.reportTypeSmallName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障描述">
          {{ reportOrderDetail.reportDesc }}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions style="margin-top: 10px" title="处理状态" :column="3" size="mini" border>
        <el-descriptions-item label="报障单状态">
          {{ getDicLabelByValue(this.dict.type.report_status, reportOrderDetail.reportStatus) }}
        </el-descriptions-item>
        <el-descriptions-item label="处理次数">
          {{ reportOrderDetail.reportDealCount }}
        </el-descriptions-item>
        <el-descriptions-item label="处理人">
          {{ reportOrderDetail.reportDealUserName }}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions v-if="reportOrderDetail.reportFiles" style="margin-top: 10px" title="附件信息" :column="1"
                       size="mini" border>
        <el-descriptions-item v-for="(url,index) in reportOrderDetail.reportFiles.split(';')" :label="'附件'+index">
          <a :href="getBaseUrl(url)">下载查看</a>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <el-dialog :visible.sync="dealOrderOpen" width="800px" id="dealOrderDialog" :close-on-click-modal="false">
      <el-descriptions class="margin-top" title="报障单详情" :column="3" size="mini" border>
        <el-descriptions-item label="报障人">
          {{ dealOrderDetail.reportUserName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障地点">
          {{ dealOrderDetail.reportLocation }}
        </el-descriptions-item>
        <el-descriptions-item>
        </el-descriptions-item>
        <el-descriptions-item label="手机号">
          {{ dealOrderDetail.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          {{ dealOrderDetail.email }}
        </el-descriptions-item>
        <el-descriptions-item label="qq号">
          {{ dealOrderDetail.qq }}
        </el-descriptions-item>
        <el-descriptions-item label="故障标题">
          {{ dealOrderDetail.reportTitle }}
        </el-descriptions-item>
        <el-descriptions-item label="故障大类">
          {{ dealOrderDetail.reportTypeBigName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障小类">
          {{ dealOrderDetail.reportTypeSmallName }}
        </el-descriptions-item>
        <el-descriptions-item label="故障描述">
          {{ dealOrderDetail.reportDesc }}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions style="margin-top: 10px" title="处理状态" :column="3" size="mini" border>
        <el-descriptions-item label="报障单状态">
          {{ getDicLabelByValue(this.dict.type.report_status, dealOrderDetail.reportStatus) }}
        </el-descriptions-item>
        <el-descriptions-item label="处理次数">
          {{ dealOrderDetail.reportDealCount }}
        </el-descriptions-item>
        <el-descriptions-item label="处理人">
          {{ dealOrderDetail.reportDealUserName }}
        </el-descriptions-item>
      </el-descriptions>
      <el-form :rules="rules" ref="dealForm" :model="dealForm">
        <el-form-item label="问题原因" prop="reportReason">
          <el-input type="textarea" v-model="dealForm.reportReason"></el-input>
        </el-form-item>
        <el-form-item label="解决方案" prop="reportSolution">
          <el-input type="textarea" v-model="dealForm.reportSolution"></el-input>
        </el-form-item>
      </el-form>
      <div style="margin-top: 10px;text-align: right">
        <el-button type="primary" @click="orderDealSubmit">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="transferDialogOpen" title="报障单处理人转移" width="300px">
      <el-form :rules="transferFormRule" ref="transferReportForm" :model="transferForm">
        <el-form-item label="接收人" prop="toUserId">
          <el-select
            v-model="transferForm.toUserId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in transferToUserList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="transferForm.remark" type="textarea" placeholder="请输入转移备注"/>
        </el-form-item>
      </el-form>
      <div style="margin-top: 10px;text-align: right">
        <el-button type="primary" @click="transferDealUserSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {parseTime} from "@/utils/ruoyi";
import request from "@/utils/request";
import reportTimeLine from "@/views/report/comp/reportTimeLine";
import {transferReportOrder} from "@/api/report/ReportOrder";

export default {
  name: "MyReportOrder",
  dicts: ['report_status'],
  components: {
    reportTimeLine
  },
  data() {
    return {
      transferToUserList: [],

      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报障单表格数据
      ReportOrderList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportTitle: null,
        reportUserId: null,
        reportStatus: ['DEALING', "BACKING"]
      },
      historyDrawerOpen: false,
      timeLineItemList: [],
      reportOrderDetail: {},
      reportOrderDetailOpen: false,
      dealOrderDetail: {},
      dealOrderOpen: false,
      rules: {
        reportSolution: [
          {required: true, message: "解决方案不能为空", trigger: "blur"}
        ],
        reportReason: [
          {required: true, message: "解问题原因不能为空", trigger: "blur"}
        ],
      },
      dealForm: {
        reportSolution: "",
        reportReason: ""
      },
      transferDialogOpen: false,
      transferForm: {
        id: "",
        toUserId: null,
        remark: ""
      },
      transferFormRule: {
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
        toUserId: [
          {required: true, message: "接收人", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    remoteMethod(query) {
      if (query !== '') {
        request({
          url: '/report/ReportOrder/queryDealUser?query=' + encodeURIComponent(query),
          method: 'get'
        }).then(res => {
          this.transferToUserList = res.data
        })
      } else {
        this.transferToUserList = [];
      }
    },
    dealTransfer(id) {
      this.transferForm = {
        id: id,
        toUserId: null,
        remark: ""
      }
      this.transferDialogOpen = true
    },
    orderDealSubmit() {
      this.$refs["dealForm"].validate(valid => {
        if (valid) {
          this.$confirm('确认已完成吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(res => {
            var dialogLoadingInstance = this.$loading({
              visible: true,
              text: '处理中',
              spinner: 'el-icon-loading',
              target: '#dealOrderDialog'
            })
            request({
              url: '/report/ReportOrder/complete',
              method: 'post',
              data: {
                id: this.dealOrderDetail.id,
                reportReason: this.dealForm.reportReason,
                reportSolution: this.dealForm.reportSolution,
              }
            }).then(res => {
              this.$message.success("处理成功")
              this.getList()
              dialogLoadingInstance.close()
              this.dealOrderOpen = false
            }).catch(res => {
              dialogLoadingInstance.close()
            })
          }).catch(res => {
          })
        }
      })
    },
    dealOrder(orderId) {
      this.loading = true
      request({
        url: '/report/ReportOrder/noAuth/' + orderId,
        method: 'get'
      }).then(res => {
        this.dealOrderDetail = res.data
        this.dealForm.reportReason = res.data.reportReason
        this.dealForm.reportSolution = res.data.reportSolution
        this.dealOrderOpen = true
        this.loading = false
      }).catch(res => {
        this.loading = false
      });
    },
    getOrderBtn() {
      this.getOrder(this.reportOrderDetail.id)
    },
    getBaseUrl(url) {
      return process.env.VUE_APP_BASE_API + '/file/download?path=' + url
    },
    /** 查询报障单列表 */
    getList() {
      this.loading = true;
      request({
        url: '/report/ReportOrder/listMyOrder',
        method: 'get',
        params: this.queryParams
      }).then(response => {
        this.ReportOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情操作 */
    handleDetail(row) {
      var id = row.id
      request({
        url: '/report/ReportOrder/noAuth/' + id,
        method: 'get'
      }).then(res => {
        this.reportOrderDetail = res.data
        this.reportOrderDetailOpen = true
      })
    },
    handleHistory(row) {
      const id = row.id
      request({
        url: '/report/ReportOrder/reportHistoryNoAuth/' + id,
        method: 'get'
      }).then(res => {
        this.timeLineItemList = res.data
      })
      //查询历史，右侧显示流程
      this.historyDrawerOpen = true
    },
    transferDealUserSubmit() {
      this.$refs['transferReportForm'].validate(valid => {
        if (valid) {
          this.$confirm('确认转移吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(res => {
            this.$modal.loading("正在转移")
            transferReportOrder(this.transferForm).then(res => {
              this.$modal.closeLoading()
              this.$message.success("转移成功")
              this.transferDialogOpen = false
              this.getList()
            })
          })

        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/ReportOrder/export', {
        ...this.queryParams
      }, `ReportOrder_${new Date().getTime()}.xlsx`)
    }
  },
  activated() {
    let refresh = this.$route.query.refresh
    if (refresh == "true") {
      this.getList()
    }
  },
};
</script>
